package com.lx.config;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(WxPayConfig.class) // 添加这个注解
@ConfigurationProperties(prefix = "wx")
public class WxPayConfig {

    private String merchantId;
    private String privateKeyPath;
    private String merchantSerialNumber;
    private String apiV3Key;
    private String appid;
    private String notifyUrl;

    @Bean
    public RSAAutoCertificateConfig rsaAutoCertificateConfig() {
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(getAbsolutePrivateKeyPath())
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .build();
    }
    public RSAAutoCertificateConfig getConfig() {
        return rsaAutoCertificateConfig();
    }

    private String getAbsolutePrivateKeyPath() {
        if (privateKeyPath.startsWith("classpath:")) {
            String resourcePath = privateKeyPath.substring("classpath:".length());
            return getClass().getClassLoader().getResource(resourcePath).getPath();
        }
        return privateKeyPath;
    }

    public String getApiV3Key() {
        return apiV3Key;
    }

    public void setApiV3Key(String apiV3Key) {
        this.apiV3Key = apiV3Key;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(String merchantId) {
        this.merchantId = merchantId;
    }

    public String getMerchantSerialNumber() {
        return merchantSerialNumber;
    }

    public void setMerchantSerialNumber(String merchantSerialNumber) {
        this.merchantSerialNumber = merchantSerialNumber;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getPrivateKeyPath() {
        return privateKeyPath;
    }

    public void setPrivateKeyPath(String privateKeyPath) {
        this.privateKeyPath = privateKeyPath;
    }
}